package com.wolfsea.curveviewcase
import android.content.Context
import android.graphics.Canvas
import android.graphics.Paint
import android.graphics.Path
import android.util.AttributeSet
import android.view.View

/**
 *@desc  SinView
 *@author liuliheng
 *@time 2022/12/11  22:34
 **/
class SinView @JvmOverloads constructor(context: Context, attributeSet: AttributeSet? = null) :
    View(context, attributeSet) {

    private val sinPaint by lazy {
        Paint().apply {
            isDither = true
            isAntiAlias = true
            color = context.resources.getColor(R.color.purple_200)
            style = Paint.Style.STROKE
            strokeWidth = context.resources.getDimension(R.dimen.dp_1)
        }
    }

    private val pointPaint by lazy {
        Paint().apply {
            isDither = true
            isAntiAlias = true
            color = context.resources.getColor(R.color.teal_200)
            style = Paint.Style.FILL
            strokeWidth = context.resources.getDimension(R.dimen.dp_5)
        }
    }

    private val path by lazy {
        Path()
    }

    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec)
        setMeasuredDimension(
            MeasureSpec.getSize(widthMeasureSpec),
            MeasureSpec.getSize(heightMeasureSpec)
        )
    }

    override fun onDraw(canvas: Canvas?) {
        super.onDraw(canvas)
        canvas?.apply {
            translate(width.toFloat() / 2, height.toFloat() / 2)
            path.moveTo(-width.toFloat() / 2,0F)
            //绝对点
            /*for (i in 0.until(TOTAL_COUNT) step 4) {
                if ((i + 2) < TOTAL_COUNT) {
                    path.quadTo(
                        -width.toFloat() / 2 + QUARTER_WAVE_LENGTH * (i + 1),
                        -WAVE_HEIGHT,
                        -width.toFloat() / 2 + QUARTER_WAVE_LENGTH * (i + 2),
                        0F
                    )
                }
                if ((i + 4) < TOTAL_COUNT) {
                    path.quadTo(
                        -width.toFloat() / 2 + QUARTER_WAVE_LENGTH * (i + 3),
                        WAVE_HEIGHT,
                        -width.toFloat() / 2 + QUARTER_WAVE_LENGTH * (i + 4),
                        0F
                    )
                }
            }*/

            //相对点
            /*第一种方式*/
            /*for (i in 0.until(3)) {
                path.rQuadTo(
                    QUARTER_WAVE_LENGTH * 1,
                    -WAVE_HEIGHT,
                    QUARTER_WAVE_LENGTH * 2,
                    0F
                )
                path.rQuadTo(
                    QUARTER_WAVE_LENGTH * 1,
                    WAVE_HEIGHT,
                    QUARTER_WAVE_LENGTH * 2,
                    0F
                )
            }*/

            /*第二种方式*/
            for (i in 0.until(7)) {
                path.rQuadTo(
                    QUARTER_WAVE_LENGTH * 1,
                    -WAVE_HEIGHT * if (i % 2 == 0) 1 else -1,
                    QUARTER_WAVE_LENGTH * 2,
                    0F
                )
            }

            drawPath(path, sinPaint)

            //在曲线上绘制点
            drawPoint(
                -width.toFloat() / 2 + QUARTER_WAVE_LENGTH * 1 / 4F,
                -WAVE_HEIGHT / 4F,
                pointPaint
            )
        }
    }

    companion object {
        const val WAVE_HEIGHT = 80F
        const val QUARTER_WAVE_LENGTH = 45F
        const val TOTAL_COUNT = 15
    }

}